package com.mars.module.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mars.common.response.PageInfo;
import com.mars.framework.context.ContextUserInfoThreadHolder;
import com.mars.module.admin.entity.*;
import com.mars.module.admin.mapper.SysScenicAreaMapper;
import com.mars.module.admin.mapper.SysScenicSpotMapper;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import com.mars.module.admin.request.SysSpotTicketRequest;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import com.mars.module.admin.mapper.SysSpotTicketMapper;
import org.springframework.beans.BeanUtils;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.mars.module.admin.service.ISysSpotTicketService;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 景点门票业务层处理
 *
 * @author mars
 * @date 2024-03-26
 */
@Slf4j
@Service
@AllArgsConstructor
public class SysSpotTicketServiceImpl implements ISysSpotTicketService {

    private final SysSpotTicketMapper baseMapper;
    private final SysScenicSpotMapper spotMapper;
    private final SysScenicAreaMapper areaMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public SysSpotTicket add(SysSpotTicketRequest request) {
        SysSpotTicket entity = SysSpotTicket.builder().build();
        BeanUtils.copyProperties(request, entity);
        Long userId = ContextUserInfoThreadHolder.get().getId();
        entity.setCreateById(userId);
        baseMapper.insert(entity);
        return entity;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean delete(Long id) {
        return baseMapper.deleteById(id) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteBatch(List<Long> ids) {
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean update(SysSpotTicketRequest request) {
        SysSpotTicket entity = SysSpotTicket.builder().build();
        BeanUtils.copyProperties(request, entity);
        return baseMapper.updateById(entity) > 0;
    }

    @Override
    public SysSpotTicket getById(Long id) {
        return baseMapper.selectById(id);
    }

    @Override
    public PageInfo<SysSpotTicket> pageList(SysSpotTicketRequest request) {
        Page<SysSpotTicket> page = new Page<>(request.getPageNo(), request.getPageSize());
        LambdaQueryWrapper<SysSpotTicket> query = this.buildWrapper(request);
        IPage<SysSpotTicket> pageRecord = baseMapper.selectPage(page, query);
        List<SysSpotTicket> records = pageRecord.getRecords();
        List<SysSpotTicket> recordsNew = new ArrayList<>();
        for (SysSpotTicket e : records) {
            if ("1".equals(e.getTicketType())) {
                SysScenicArea area = areaMapper.selectById(e.getSourceId());
                e.setSourceName(area == null ? "" : area.getName());
                e.setTicketType("景区门票");
            } else if ("2".equals(e.getTicketType())) {
                SysScenicSpot scenicSpot = spotMapper.selectById(e.getSourceId());
                e.setSourceName(scenicSpot == null ? "" : scenicSpot.getName());
                e.setTicketType("景点门票");
            }
            recordsNew.add(e);
        }
        pageRecord.setRecords(recordsNew);
        return PageInfo.build(pageRecord);
    }


    @Override
    public List<SysSpotTicket> list(SysSpotTicketRequest request) {
        LambdaQueryWrapper<SysSpotTicket> query = this.buildWrapper(request);
        return baseMapper.selectList(query);
    }

    private LambdaQueryWrapper<SysSpotTicket> buildWrapper(SysSpotTicketRequest param) {
        LambdaQueryWrapper<SysSpotTicket> query = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(param.getName())) {
            query.like(SysSpotTicket::getName, param.getName());
        }
        if (StringUtils.isNotBlank(param.getContent())) {
            query.like(SysSpotTicket::getContent, param.getContent());
        }
        if (StringUtils.isNotBlank(param.getType())) {
            query.like(SysSpotTicket::getType, param.getType());
        }
        if (StringUtils.isNotBlank(param.getTicketType())) {
            query.eq(SysSpotTicket::getTicketType, param.getTicketType());
        }
        if (param.getSourceId() != null) {
            query.eq(SysSpotTicket::getSourceId, param.getSourceId());
        }
        return query;
    }

}
